<template>
  <el-container>
    <el-main>
      <div class="course-content">
        <div class="courer-operation">
          <div class="operation-left">
            <el-button type="primary" @click="goTo('create')">新建课程</el-button>
            <el-button type="primary" @click="goTo('type')">录播课分类</el-button>
            <el-button type="primary" @click="goTo('courseComment')">课程评论</el-button>
            <el-button type="primary" @click="goTo('timeComment')">课时评论</el-button>
            <el-button type="primary" @click="goTo('import')">课时批量导入</el-button>
            <div class="options-link">
              <div class="link">
                <img alt="" src="@/assets/images/config/icon-option.png" />
                录播课设置
              </div>
            </div>
          </div>
          <div class="operation-right">
            <div class="key-ipt">
              <el-input v-model="form.name" placeholder="课程名称关键字"></el-input>
            </div>
            <div class="btn">
              <el-button @click="clear">清空</el-button>
              <el-button type="primary" @click="choose">筛选</el-button>
            </div>
            <div class="more" @click="drawer = true" v-if="!show">
              <img src="@/assets/img/icon-filter.png" alt="" />
              <span>更多</span>
            </div>
            <div class="more" @click="drawer = true" v-else>
              <img src="@/assets/img/icon-filter-h.png" alt="" />
              <span style="color: #3ca7fa">已选</span>
            </div>
          </div>
        </div>
        <div class="course-table" id="course">
          <el-table :data="allCourseData" style="width: 100%" :default-sort="{ prop: 'id', order: 'descending' }"
            @sort-change="changeSort">
            <el-table-column prop="id" label="ID" :sortable="'custom'" min-width="71"></el-table-column>
            <el-table-column prop="thumb,title" label="课程" min-width="336">
              <template slot-scope="scope">
                <img :src="scope.row.thumb" alt="" width="120px" height="90px" style="border-radius: 4px" />
                <span class="title">{{ scope.row.title }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="category.name" label="分类" min-width="134"></el-table-column>
            <el-table-column prop="charge" label="价格" :sortable="'custom'" min-width="89">
              <template slot-scope="scope">
                <span>{{ scope.row.charge + `元` }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="user_count" label="销量" sortable width="89"></el-table-column>
            <el-table-column prop="published_at" label="上架时间" sortable min-width="156">
              <template slot-scope="scope">
                <span>{{
                    scope.row.published_at.substring(
                      0,
                      scope.row.published_at.lastIndexOf(":")
                    )
                }}
                </span>
              </template>
            </el-table-column>
            <el-table-column label="是否显示" miin-width="89">
              <template slot-scope="scope">
                <span v-if="scope.row.is_show == '1'" style="color: #04c877">· 显示</span>
                <span v-else style="color: #ff4d4f">· 隐藏</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" min-width="156">
              <template slot-scope="scope">
                <el-link type="primary" @click="goToVodManagement(scope.row.id)">课时</el-link>
                <el-link type="primary" class="member">学员</el-link>
                <el-link type="primary">
                  <el-dropdown :hide-on-click="false" trigger="hover" @command="handleCommand">
                    <span class="el-dropdown-link">
                      更多<i class="el-icon-arrow-down el-icon--right"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item style="color: #66b1ff" :command="`edit-${scope.row.id}`">编辑</el-dropdown-item>
                      <el-dropdown-item style="color: #66b1ff" :command="`enclosure-${scope.row.id}`">附件
                      </el-dropdown-item>
                      <el-dropdown-item style="color: #f78989" :command="`delete-${scope.row.id}`">删除</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-link>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination">
          <div class="block">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
              :current-page="currentPage_1" :page-sizes="[10, 20, 50, 100]" :page-size="10"
              layout="total, sizes, prev, pager, next, jumper" :total="totalSize">
            </el-pagination>
          </div>
        </div>
      </div>
    </el-main>
    <div class="drawer" style="position: relative">
      <el-drawer :visible.sync="drawer" :before-close="handleClose" :show-close="false">
        <div class="choose">
          <div class="title">更多筛选</div>
          <div class="content">
            <el-input v-model="form.name" placeholder="课程名称关键字"></el-input>
            <el-select v-model="type" placeholder="分类">
              <div v-for="item in categories" :key="item.name">
                <el-option :label="item.name" :value="item.name"> </el-option>
                <el-option v-for="v in item.children" :key="v.name" :lable="v.name" :value="'|----' + v.name">
                </el-option>
              </div>
            </el-select>
            <el-input v-model="courseId" placeholder="课程ID"></el-input>
            <div class="btn">
              <el-button @click="clear">清空</el-button>
              <el-button type="primary" @click="choose">筛选</el-button>
            </div>
          </div>
        </div>
      </el-drawer>
    </div>
  </el-container>
</template>

<script>
import http from "@/network/http";
import { course, create } from "@/network/course";
import { Loading } from "element-ui";
export default {
  data() {
    return {
      form: {
        name: "",
      },
      coursePage: 1,
      courseSize: 10,
      sort: "id",
      order: "desc",
      allCourseData: [],
      // 记录分页条数 和 页数
      totalSize: 1,
      currentPage_1: 1,
      drawer: false,
      type: "",
      courseId: "",
      categories: [],
      value: "",
      id: 0,
      categoryId: 0,
      show: false,
    };
  },
  methods: {
    // 获取id
    getTypeId: function () {
      for (let i = 0; i < this.categories.length; i++) {
        if (this.categories[i].children.length == 0) {
          if (this.type.includes(this.categories[i].name)) {
            this.categoryId = this.categories[i].id;
            return;
          }
        } else {
          for (let j = 0; j < this.categories[i].children.length; j++) {
            if (this.type.includes(this.categories[i].children[j].name)) {
              this.categoryId = this.categories[i].children[j].id;
              return;
            } else if (this.type.includes(this.categories[i].name)) {
              this.categoryId = this.categories[i].id;
              return;
            }
          }
        }
      }
    },
    getCourseData: function () {
      // this.loading && this.loading.close();
      let loading = Loading.service({
        target: ".course-table",
      });
      let params = {
        page: this.coursePage,
        size: this.courseSize,
        sort: this.sort,
        order: this.order,
      };
      if (this.form.name.length != 0) {
        params["keywords"] = this.form.name;
      }
      if (this.type.length != 0) {
        this.getTypeId();
        params["cid"] = this.categoryId;
      }
      if (this.courseId.length != 0) {
        params["id"] = this.courseId;
      }
      course(params).then((res) => {
        this.allCourseData = res?.data?.courses?.data;
        this.totalSize = res?.data?.courses?.total;
        // 关闭加载
        loading.close();
      });
    },
    handleCommand(command) {
      console.log("click: ", command);
      if (command.includes("edit")) {
        this.id = command.split("-")[1];
        this.$router.push({
          path: "/course/vod/update",
          query: {
            id: this.id,
          },
        });
      }
      if (command.includes("enclosure")) {
        this.id = command.split("-")[1];
        this.$router.push({
          path: "/course/vod/attach/index",
          query: {
            course_id: this.id,
          },
        });
      }
      if (command.includes("delete")) {
        this.id = command.split("-")[1];
        this.$confirm("确认操作?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            http.delete(`/course/${this.id}`).then((res) => {
              this.getCourseData();
            });
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          })
          .catch(() => {
            // this.$message({
            //   type: "info",
            //   message: "已取消删除",
            // });
          });
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.courseSize = val;
      this.getCourseData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.coursePage = val;
      this.getCourseData();
    },
    changeSort(val) {
      // console.log(val);
      if (val.prop == "id") {
        this.sort = "id";
      }
      if (val.prop == "charge") {
        this.sort = "charge";
      }
      if (val.prop == "user_count") {
        this.sort = "user_count";
      }
      if (val.order == "ascending") {
        this.order = "asc";
        this.getCourseData();
      } else if (val.order == "descending") {
        this.order = "desc";
        this.getCourseData();
      } else {
        this.getCourseData();
      }
    },
    goTo: function (name) {
      if (name === "create") {
        this.$router.push({
          path: "/course/vod/create",
        });
      }
      if (name == "type") {
        this.$router.push({
          path: "/course/vod/category/index",
        });
      }
      if (name == "courseComment") {
        this.$router.push({
          path: "/course/vod/components/vod-comments",
        });
      }
      if (name == "timeComment") {
        this.$router.push({
          path: "/course/vod/video/comments",
        });
      }
      if (name == "import") {
        this.$router.push({
          path: "/course/vod/video-import",
        });
      }
    },
    handleClose(done) {
      // this.$confirm("确认关闭？")
      //   .then((_) => {
      //     done();
      //   })
      //   .catch((_) => {});
      done();
    },
    choose: function () {
      this.getCourseData();
      this.show = true;
      this.drawer = false;
    },
    clear: function () {
      this.form.name = "";
      this.courseId = "";
      this.type = "";
      this.getCourseData();
      this.show = false;
      this.drawer = false;
    },
    goToVodManagement: function (id) {
      // console.log(id);
      this.$router.push({
        path: "/course/vod/video/index",
        query: {
          course_id: id,
        },
      });
    },
  },
  created() {
    this.getCourseData();
    create().then((res) => {
      this.categories = res?.data?.categories;
      // console.log(this.categories);
    });
  },
};
</script>

<style lang="scss" scoped>
.el-container {
  height: 100%;
  text-align: center;
}

.el-main {
  background-color: #f1f2f9;
  // line-height: 200px;
  padding: 20px 5px 20px 20px;
  overflow-y: auto;

  .course-content {
    width: 100%;
    height: auto;
    // float: left;
    background-color: #fff;
    box-sizing: border-box;
    padding: 30px;
    border-radius: 15px;
    // margin-bottom: 90px;
    box-shadow: 0 2px 4px 0 hsl(0deg 0% 40% / 5%);
    min-width: 1180px;
  }

  .courer-operation {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 30px;

    .operation-left {
      display: flex;
      align-items: center;

      .options-link {
        display: inline-block;
        vertical-align: middle;
        width: auto;
        height: 30px;
        box-sizing: border-box;
        padding: 0 0 0 20px;
        border-left: 1px solid #d8d8d8;
        margin-left: 30px;

        .link {
          width: 100%;
          height: 100%;
          cursor: pointer;
          font-size: 16px;
          font-weight: 400;
          color: #606266;
          display: flex;
          flex-direction: row;
          align-items: center;

          img {
            width: 30px;
            height: 30px;
            margin-right: 10px;
          }
        }
      }
    }

    .operation-right {
      display: flex;
      align-items: center;
      height: 40px;
      line-height: 40px;

      .key-ipt {
        margin-right: 10px;
        height: 40px;

        .el-input {
          width: 150px;
        }
      }

      // .btn {
      //   .el-button {
      //   }
      // }

      .more {
        height: 14px;
        font-size: 14px;
        font-weight: 400;
        color: #666;
        line-height: 14px;
        margin-left: 10px;
        cursor: pointer;

        img {
          width: 14px;
          height: 14px;
          margin-right: 5px;
        }

        // span {
        // }
      }
    }
  }

  ::v-deep .course-table {

    // 表格内容深度设置
    .el-table th {
      line-height: 0;
      background: rgb(241, 242, 249) !important;
      color: #333;
    }

    .el-table th:last-of-type>.cell {
      justify-content: flex-end;
    }

    .el-table td.el-table__cell div {
      display: flex;
      align-items: center;
    }

    .el-table .cell {
      color: #333;
      display: flex;
      align-items: center;
    }

    .el-table td:last-child .cell {
      justify-content: flex-end !important;
    }

    .title {
      margin-left: 10px;
      max-width: 340px;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
    }

    .member {
      margin-left: 5px;
      margin-right: 5px;
    }

    // 下拉菜单的样式
    .el-dropdown-link {
      cursor: pointer;
      color: #409eff;
    }

    .el-icon-arrow-down {
      font-size: 12px;
    }
  }

  // 分页
  .pagination {
    margin-top: 30px;
  }
}

div.el-dialog__wrapper {
  right: 0;
  top: 0;
  bottom: 0;
  left: 0;
}

::v-deep .el-drawer {
  width: 360px !important;
}

::v-deep .el-drawer__header {
  margin: 0;
  padding: 0;
}

.choose {
  width: 100%;
  height: auto;
  float: left;
  padding: 30px;

  .title {
    width: 100%;
    height: 16px;
    font-size: 16px;
    font-weight: 400;
    color: #333;
    line-height: 16px;
    margin-bottom: 30px;
    display: flex;
  }

  .content {
    .el-select {
      width: 100%;
      margin: 20px 0;
    }

    .btn {
      display: flex;
      justify-content: space-between;
      margin-top: 30px;
    }
  }
}
</style>